﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcEngine.Core.Services;
using MvcEngine.Core.Localization;

using MvcEngine.Core;
using MvcEngine.Blog.Models.Entities;
using MvcEngine.Core.Helpers;
using MvcEngine.Blog.Repositories;
using System.Web.Mvc;
using MvcEngine.Blog.Components.Extensions;
using MvcEngine.Blog.Models.ViewData;
using MvcEngine.Blog.Managers;

namespace MvcEngine.Blog.Helper
{
    public class BlogEmailHelper : IBlogEmailHelper
    {
        public static IBlogEmailHelper Current
        {
            get { return IoC.Resolve<IBlogEmailHelper>(); }
        }

        #region IBlogEmailHelper Members

        public void SendPostNotification(string email, string author, string content, string postLink, string postTitle)
        {
            Guard.ThrowIfEmptyString(email, "email");
            Guard.ThrowIfEmptyString(author, "author");
            Guard.ThrowIfEmptyString(content, "content");
            Guard.ThrowIfEmptyString(postLink, "postLink");
            Guard.ThrowIfEmptyString(postTitle, "postName");

            EmailSettings settings = IoC.Resolve<IEmailService>("BlogEmailService").GetSettings();
            BlogData blog = BlogManager.Current.GetCurrentBlog();

            string body = EmailSender.Current.GetMailBody("MailPostNotificationTemplate",
                "author", author,
                "content", content,
                "link", postLink,
                "title", postTitle);

            string title = EmailSender.Current.GetMailBody("MailPostNotificationTitle", "title", postTitle);

            if (!string.IsNullOrEmpty(blog.EmailSubjectPrefix))
                title = blog.EmailSubjectPrefix + " - " + title;

            try
            {
                EmailSender.Current.SendMail(settings, settings.Email, email, title, body);
            }
            catch (Exception)
            {
#warning handle exception
            }
        }

        public void SendPostNotificationToAdmin(string author, string content, string postLink, string postTitle)
        {
            EmailSettings settings = IoC.Resolve<IEmailService>("BlogEmailService").GetSettings();
            SendPostNotification(settings.Email, author, content, postLink, postTitle);
        }

        public void SendBlogMail(string email, string name, string subject, string content, string ipAddress, string userAgent)
        {
            Guard.ThrowIfEmptyString(email, "email");
            Guard.ThrowIfEmptyString(name, "name");
            Guard.ThrowIfEmptyString(subject, "subject");
            Guard.ThrowIfEmptyString(content, "content");
            Guard.ThrowIfEmptyString(ipAddress, "ipAddress");
            Guard.ThrowIfEmptyString(userAgent, "userAgent");

            BlogData blog = BlogManager.Current.GetCurrentBlog();
            EmailSettings settings = IoC.Resolve<IEmailService>("BlogEmailService").GetSettings();

            if (!string.IsNullOrEmpty(blog.EmailSubjectPrefix))
                subject = blog.EmailSubjectPrefix + " - " + subject;

            string body = EmailSender.Current.GetMailBody("MailBlogTemplate", "name", name, "email", email, "message", content, "ipAddress", ipAddress, "userAgent", userAgent);
            string title = EmailSender.Current.GetMailBody("MailBlogTitle", "subject", subject);
            try
            {
                EmailSender.Current.SendMail(settings, settings.Email, settings.Email, title, body);
            }
            catch (Exception)
            {
#warning handle exception
            }
        }

        #endregion
    }
}
